<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/21
 * Time: 9:49
 */

include 'db_connect.php';
$currentUserName = '';
$post_details = [];
$getUserInfo = [];
//$getCurrentUserInfo = [];
$userTitle = '';
$errorMsg = '';
$hasData = false;
$post_comment = [];
$pageNow = 1;
$pageCount = 0;
$getId = '';
$postId = '';
//if (!session_id()) session_start();
//if (isset($_SESSION['user_name'])) {
//    $currentUserName = $_SESSION['user_name'];
//}
//echo $currentUserName;

function init()
{
    global $getId;
    $getId = $_GET['id'];
    //echo $getId;
    global $getUserInfo;
    global $postId;
    global $con;
    global $post_details;
    global $userTitle;
    global $post_comment;
    //global $getCurrentUserInfo;
    global $hasData;
    //$post_li_id = $_GET['id'];
//    $sql = "SELECT id FROM post_li WHERE user_id="
    $sql1 = "SELECT user_id, title, content, img, add_time FROM post_li WHERE id='$getId'";
    $sql3 = "SELECT * FROM post_comment WHERE post_li_id='$getId'";

    $post_details = $con->query($sql1);
    $post_comment1 = $con->query($sql3);
    if (mysqli_num_rows($post_comment1)) {
        $hasData = true;
        //echo "没有市局";
        //echo $hasData;
    }
    //echo var_dump($post_details);
    while ($row = mysqli_fetch_array($post_details)) {
        $userId = $row[0];
        $userTitle = $row[1];
        //echo $userTitle;
        $sql2 = "SELECT name, avatar FROM user WHERE id='$userId'";
        $getUserInfo = $con->query($sql2);
    }
//    while ($row1 = mysqli_fetch_array($post_comment)) {
//        $currentUserId = $row1[2];
//        //echo $row1[2];
//        $sql4 = "SELECT name, avatar FROM user WHERE id='$currentUserId'";
//        $getCurrentUserInfo = $con->query($sql4);
//    }

    //分页
    global $pageNow;//当前页
    //$pageNow = 1;
    global $pageCount;//总数量
    $pageSize = 3;//每页显示数量
    $rowCount = 0;//总数量
    if (!empty($_GET['pageNow'])) {
        $pageNow = $_GET['pageNow'];
        echo $pageNow;//exit;
    }
    echo $pageNow;

    //global $pageCount;//总共多少页

    $sql5 = "SELECT COUNT(id) FROM post_comment WHERE post_li_id='$getId'";
    $result1 = $con->query($sql5);

    if ($row2 = mysqli_fetch_array($result1)) {
        $rowCount = $row2[0];
    }
    //echo $rowCount;
    $pageCount = ceil($rowCount/$pageSize);
    //echo $pageCount;
    $pre = ($pageNow-1)*$pageSize;
    echo $pre;
    $sql4 = "SELECT * FROM post_comment  WHERE post_li_id='$getId' limit $pre,$pageSize";
    $post_comment = $con->query($sql4);
    $postId = '';
    if ($row = mysqli_fetch_array($post_comment)) {
        $postId = $row[1];
    }
    //echo $postId;
//    echo print_r($post_comment);

//    $pageSize = 3;//每页显示数量
//    $rowCount = 0;//总数量
//    global $pageNow;//当前页数
    //if (!session_id()) session_start();
    //echo $_SESSION['user_id'];
    //$userId = $_SESSION['user_id'];
    //获取用户名
//    $sql = "SELECT name FROM user WHERE id='$userId'";
//    $result = $con->query($sql);
    //global $userName;
//    if ($row = mysqli_fetch_array($result)) {
//        $userName = $row[0];
//    }
    //echo $userName;

//    if (!empty($_GET['pageNow'])) {
//        $pageNow = $_GET['pageNow'];
//    }
//
//    global $pageCount;//总共多少页
//
//    $sql1 = "SELECT COUNT(id) FROM post_li";
//    $result1 = $con->query($sql1);
//
//    if ($row1 = mysqli_fetch_array($result1)) {
//        $rowCount = $row1[0];
//    }
//    //echo $rowCount;
//    $pageCount = ceil($rowCount/$pageSize);
//    //echo $pageCount;
//    $pre = ($pageNow-1)*$pageSize;
//    $sql2 = "SELECT * FROM post_li limit $pre,$pageSize";
//    $postLi = $con->query($sql2);
}

function reply()
{
    global $con;
    global $errorMsg;
    $t = time();
    $content = '';
    $post_li_id = $_GET['id'];
    //echo $post_li_id;
    if (!session_id()) session_start();
    if (isset($_SESSION['user_name'])) {
        $user_id = $_SESSION['user_id'];
        //echo $user_id;
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $content = trim($_POST['replyText']);
        }
        if (!empty($content)) {
            $inserts = "INSERT INTO post_comment (post_li_id, user_id, content, add_time) VALUE ('$post_li_id', '$user_id', '$content', '$t')";
            $con->query($inserts);
            init();
        } else {
            $errorMsg = "发表内容不能为空！";
            //echo "发表内容不能为空！";
        }
    } else {
        $errorMsg = "登录后才能发表回复！";
       // echo "登录后才能发表回复！";
    }

}

function get_userName($id)
{
    global $con;
    $userName = '';
    $sql = "SELECT name, avatar FROM user WHERE id='$id'";
    $result = $con->query($sql);
    if ($row = mysqli_fetch_array($result)) {
        $userName = $row[0];
    }
    return $userName;
}
function get_userAvatar($id)
{
    global $con;
    $userAvatar = '';
    $sql = "SELECT avatar FROM user WHERE id='$id'";
    $result = $con->query($sql);
    if ($row = mysqli_fetch_array($result)) {
        $userAvatar = $row[0];
    }
    return $userAvatar;
}


function main()
{
    $post = $_POST;
    $act = '';

    isset($post['act']) && $act = $post['act'];

    if ($act == 'reply') {
        reply();
    }
    init();
}
main();
?>

<!DOCTYPE html>
<!--STATUS OK-->
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="search" type="application/opensearchdescription+xml" href="/tb/cms/content-search.xml" title="百度贴吧" />
    <meta name="keywords" content="复仇者联盟,欧美电影,电影,1资源,3资源">
    <title><?php echo $userTitle;?></title>
    <style>
        ul{
            border: 1px solid #000;
        }
        ul li{
            border: 1px solid rgba(0,0,0,1);
        }
        .error{
            color: #f00;
        }
    </style>
</head>
<body>
<?php include 'public/head.php'?>
    <ul>
        <li>
        <p>标题:<?php echo $userTitle;?></p>
        </li>
        <li>
        <?php foreach($getUserInfo as $k => $v) {?>
        头像:<img src="<?php echo $v['avatar'];?>" alt="" width="100" style="border-radius: 100%"><br />
        用户:<a href=""><?php echo $v['name'];?></a>
        <?php }?>
        </li>
        <li>
        <?php foreach($post_details as $k => $v) {?>
        <p>内容:<?php echo $v['content'];?></p>
        <?php if(!empty($v['img'])){?>
                <img src="<?php echo $v['img'];?>" alt="" width="200"><br />
        <?php }?>
        <span>发帖时间:<?php echo date("Y-m-d h:i",$v['add_time']);?></span>
        <?php }?>
        </li>
    </ul>

<?php if ($hasData) {?>
    <ul>
<!--        --><?php //foreach ($getCurrentUserInfo as $k => $v) {?>
<!--            <li>-->
<!--                头像:<img src="--><?php //echo $v['avatar']?><!--" alt="" width="200"><br />-->
<!--                用户:<a href="">--><?php //echo $v['name'];?><!--</a>-->
<!--            </li>-->
<!--        --><?php //}?>
        <?php foreach ($post_comment as $k => $v) {?>
        <li>
<!--                <p>--><?php //echo $v['post_li_id'];?><!--</p>-->
                头像:<img src="<?php echo get_userAvatar($v['user_id']);?>" alt="" width="200"><br />
                用户:<a href=""><?php echo get_userName($v['user_id']);?></a>
                <p>内容:<?php echo $v['content'];?></p>
                <span>发布时间:<?php echo date("Y-m-d h:i", $v['add_time']);?></span><br /><br />
        </li>
        <?php }?>
    </ul>
    <div>
        <?php for($i=1;$i<=$pageCount;$i++) {?>
            <?php if($pageNow == $i) {?>
                <span><?php echo $i;?></span>
            <?php } else {?>
                <a href="detail.php?id=<?php
//                foreach ($post_comment as $k => $v) {
//                    echo $v['post_li_id'];
//                }
//                if ($row = mysqli_fetch_array($post_comment)) {
//                    $postId = $row[1];
//                }
                echo $postId;
                ?>&pageNow=<?php echo $i;?>"><?php echo $i;?></a>
            <?php }?>
        <?php }?>
<!--        <a href="detail.php?pageNow=1">首页</a>-->
<!--        <a href="detail.php?pageNow=--><?php //echo $pageCount?><!--">尾页</a>--><?php //echo "当前页{$pageNow}/共{$pageCount}页"?>
    </div>
<?php }?>
    <?php if (isset($errorMsg)) {?>
        <p class="error"><?php echo $errorMsg;?></p>
    <?php }?>
    <form action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>" METHOD="post">
        <fieldset>
            <legend>发表回复</legend>
            <textarea name="replyText" id="replyText" cols="30" rows="10"></textarea><br />
            <input type="hidden" name="act" value="reply">
            <input type="submit" value="发表">
        </fieldset>
    </form>
</body>
</html>
